package com.yayohos.locationmanager.configuration;


import com.yayohos.locationmanager.providers.permissionprovider.StubPermissionProvider;

/**
 * LocationConfiguration
 */
public final class LocationConfiguration {
    private final boolean keepTracking;
    private final PermissionConfiguration permissionConfiguration;
    private final DefaultProviderConfiguration defaultProviderConfiguration;

    private LocationConfiguration(Builder builder) {
        this.keepTracking = builder.keepTracking;
        this.permissionConfiguration = builder.permissionConfiguration;
        this.defaultProviderConfiguration = builder.defaultProviderConfiguration;
    }

    /**
     * 创建Builder构造器
     *
     * @return Builder
     */
    public Builder newBuilder() {
        return new Builder()
                .keepTracking(keepTracking)
                .askForPermission(permissionConfiguration)
                .useDefaultProviders(defaultProviderConfiguration);
    }

    /**
     * 是否持续定位
     *
     * @return keepTracking
     */
    public boolean keepTracking() {
        return keepTracking;
    }

    /**
     * PermissionConfiguration
     *
     * @return permissionConfiguration
     */
    public PermissionConfiguration permissionConfiguration() {
        return permissionConfiguration;
    }

    /**
     * DefaultProviderConfiguration
     *
     * @return defaultProviderConfiguration
     */
    public DefaultProviderConfiguration defaultProviderConfiguration() {
        return defaultProviderConfiguration;
    }
    // endregion

    /**
     * 构造器
     */
    public static class Builder {
        private boolean keepTracking = Defaults.KEEP_TRACKING;
        private PermissionConfiguration permissionConfiguration;
        private DefaultProviderConfiguration defaultProviderConfiguration;

        /**
         * keepTracking
         *
         * @param keepTracking If you need to keep receiving location updates, then you need to set this as true.
         *                     Otherwise manager will be aborted after any location received.
         *                     Default is False.
         * @return Builder
         */
        public Builder keepTracking(boolean keepTracking) {
            this.keepTracking = keepTracking;
            return this;
        }

        /**
         * askForPermission
         *
         * @param permissionConfiguration This configuration is required in order to configure Permission
         *                               Request process.
         *                                If this is not set, then no permission will be requested from user and
         *                                if {@linkplain Defaults#} permissions are not granted already,
         *                                then getting location will fail silently.
         * @return Builder
         */
        public Builder askForPermission(PermissionConfiguration permissionConfiguration) {
            this.permissionConfiguration = permissionConfiguration;
            return this;
        }


        /**
         * useDefaultProviders
         *
         * @param defaultProviderConfiguration This configuration is required in order to configure
         *                                     Default Location Providers.
         *                                     If this is not set, then they will not be used.
         * @return Builder
         */
        public Builder useDefaultProviders(DefaultProviderConfiguration defaultProviderConfiguration) {
            this.defaultProviderConfiguration = defaultProviderConfiguration;
            return this;
        }

        /**
         * build
         *
         * @return LocationConfiguration
         */
        public LocationConfiguration build() {
            if (defaultProviderConfiguration == null) {
                throw new IllegalStateException("You need to specify one of the provider configurations."
                        + " Please see DefaultProviderConfiguration");
            }
            if (permissionConfiguration == null) {
                permissionConfiguration = new PermissionConfiguration.Builder()
                        .permissionProvider(new StubPermissionProvider())
                        .build();
            }
            return new LocationConfiguration(this);
        }

    }
}
